const db = wx.cloud.database();
Page({
  data: {
      
    cart: [],
    allChecked: false,
    totalNum: 0,
    openid:'',
  },

  async getData(){
    var that = this;
    await db.collection('collection').where({
          _openid:that.data.openid
      }).get().then(res=>{
              var list = res.data;
              for(var i =0;i<list.length;i++){
                list[i].checked = false;
              }
            that.setData({
                cart:list
            })
      })

 },

 onShow() {
    var u = wx.getStorageSync('userFlag'); 
    if(u.flag==0) {
        wx.showModal({
            title: '提示',
            content: '你还未完善个人信息，是否立刻去完善？',
            success (res) {
                if (res.confirm) {
                    wx.switchTab({
                        url: '/pages/personal/personal',
                    })
                } else if (res.cancel) {
                            wx.switchTab({
                                url: '/pages/index/index',
                            })
                        }
                }
        })
    }else{
        this.setData({
            openid:u.user._id,
        })
    }

    this.getData();
 },

 goto(e){
  var str = e.currentTarget.dataset.record.object;
  var different = e.currentTarget.dataset.record.condition
  wx.navigateTo({
    url: '/pages/show/show?jsonStr=' + encodeURIComponent(JSON.stringify(str))+'&condition='+ different,
  })
 },
  // 商品的选中
  handeItemChange(e) {
    const goods_id = e.currentTarget.dataset.id;
    let { cart } = this.data;
    let index = cart.findIndex(v => v._id === goods_id);

    cart[index].checked = !cart[index].checked;

    this.setCart(cart);

  },
  setCart(cart) {
    let allChecked = true;

    let totalNum = 0;
    cart.forEach(v => {
      if (v.checked) {
        totalNum += 1;
      } else {
        allChecked = false;
      }
    })
    // 判断数组是否为空
    allChecked = cart.length != 0 ? allChecked : false;
    this.setData({
       cart,
       totalNum, 
       allChecked
    });
    
  },

  // 商品全选功能
  handleItemAllCheck() {
    // 1 获取data中的数据
    let { cart, allChecked } = this.data;
    // 2 修改值
    allChecked = !allChecked;
    // 3 循环修改cart数组 中的商品选中状态
    cart.forEach(v => v.checked = allChecked);
    // 4 把修改后的值 填充回data或者缓存中
    this.setCart(cart);
  },

  async delete(){
    var that = this;
    const {totalNum}=this.data;

    // 2 判断用户有没有选购商品
    if(totalNum===0){
      wx.showToast({
          title:"您还没有选择任何记录",
          icon:'none'
        });
      return ;
    } 
    var {cart} = this.data;
    var idlist = [];
    cart.forEach(element=>{
      if(element.checked){
        idlist.push(element._id);
      }
    })
    for(var i =0;i<idlist.length;i++){
      await this.remove(idlist[i])
    }
    
    wx.showToast({
      title: '取消收藏',
    })
    
    await that.getData();
    let l = that.data.cart;
    that.setCart(l);
  },

  async remove(id){
      await db.collection('collection').doc(id).remove().then(res=>{
    });
  },
  onPullDownRefresh:function () {
    setTimeout(()=>{ wx.stopPullDownRefresh({
    success: (res) => {}})},1000)  
}
})